/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package docmanager.business.objects;

import java.sql.*;

import bf.*;
import bf.operation.Operation;

import java.util.*;


/**
 *
 * @author Juan Lucas
 */
public class Rol extends BusinessObject
{
    String nombre;
    boolean completeControl;
    List operaciones;
    
    
    public static final int SYSOP = 1;
    public static final int CGC = 2;
    public static final int JEFE_SECTOR = 3;
    public static final int SUPERVISOR = 4;
    public static final int ADMINISTRATIVO = 5;
    public static final int OPERARIO = 6;


	public Rol(int id, String nombre, boolean completeControl)
    {
        this.id = id;
        this.nombre = nombre;
        this.completeControl = completeControl;
    }
    
    public Rol(Connection conn, int id, String nombre, boolean completeControl)
    {
        this.id = id;
        this.nombre = nombre;
        this.completeControl = completeControl;
        this.conn = conn;
    }
    
    
    public Rol(Connection conn, int id) throws SQLException
    {
      this.conn = conn;
      this.id = id;
      String query = "SELECT nombre,complete_control FROM roles WHERE id="+id;
      ResultSet rs = DataAccess.selectQueryStatic(conn, query);
      rs.next();
      this.nombre = rs.getString("nombre");
      int temp = rs.getInt("complete_control");
      if (temp == 1)
      {
          this.completeControl = true;
      }
      else
      {
          this.completeControl = false;
      }
    }
    
    
    public boolean isCompleteControl()
    {
        return completeControl;
    }

    public void setCompleteControl(boolean completeControl)
    {
        this.completeControl = completeControl;
    }

    public String getNombre()
    {
        return nombre;
    }

    public void setNombre(String nombre)
    {
        this.nombre = nombre;
    }

    
    public List getOperaciones() {
		return operaciones;
	}

	public void setOperaciones(List operaciones) {
		this.operaciones = operaciones;
	}
    
    @Override
    public void guardarNuevo() throws SQLException
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    
    public String toString()
    {
        String s = this.nombre;
        if (this.isCompleteControl())
            s = s + "(*)";
        return s;
    }
    
    public static String listarRoles(List l)
    {
        String s = "";
        Iterator it = l.iterator();
        boolean first = true;
        while (it.hasNext())
        {
            if(!first)
                s = s + ", ";
            Rol r = (Rol) it.next();
            s = s + r.toString();
            first = false;
        }
        return s;
    }
    
    
    public List cargarOperaciones() throws SQLException
    {
        String query = "SELECT operaciones.id as id, operaciones.nombre as nombre "
                + "FROM operaciones, roles, operaciones_roles "
                + "WHERE operaciones.id = operaciones_roles.id_operacion and roles.id = operaciones_roles.id_rol and roles.id = " +this.getId();
          List l = new LinkedList();
          System.out.println("QUERY: " + query);
          ResultSet rs = DataAccess.selectQueryStatic(conn, query);
          while (rs.next())
          {
            int id = rs.getInt("id");
            String nombre = rs.getString("nombre");
            l.add(new OperacionDB(id,nombre));
      }
      this.operaciones = l;
      return(l);
 
    }
    
    public Set obtenerIdOperaciones() throws SQLException
    {
        if (this.operaciones == null)
            this.operaciones = this.cargarOperaciones();
        Set s = new HashSet();
        Iterator it = this.operaciones.iterator();
        while (it.hasNext())
        {
            OperacionDB op = (OperacionDB) it.next();
            s.add(new Integer(op.getId()));
        }
        return s;
    }
    
    public void modificar(Connection conn) throws SQLException
    {
        int compCont = 0;
        if (this.isCompleteControl())
        	compCont = 1;
    	String query = "UPDATE roles "
               + "SET nombre='" + this.getNombre()+ "', " +"complete_control='"+compCont+"' "
               + "WHERE id=" + this.getId();
       DataAccess.dmlQueryStatic(conn,query);        
    }
    
    public void modificar() throws SQLException
    {
    	this.modificar(this.conn);
    }
    
    public static List obtenerTodosRoles(Connection conn) throws SQLException
    {
      String query = "SELECT id, nombre, complete_control FROM roles";
      List l = new LinkedList();
      ResultSet rs = DataAccess.selectQueryStatic(conn, query);
      while (rs.next())
      {
          int id = rs.getInt("id");;
          String nombre = rs.getString("nombre");
          int temp = rs.getInt("complete_control");
          boolean cc;
          if (temp == 1)
              cc = true;
          else
              cc = false;
          l.add(new Rol(conn,id,nombre,cc));
      }
      return(l);
    }

    public static List obtenerRolesComunes(Connection conn) throws SQLException
    {
      String query = "SELECT id, nombre, complete_control FROM roles";
      List l = new LinkedList();
      ResultSet rs = DataAccess.selectQueryStatic(conn, query);
      while (rs.next())
      {
          int id = rs.getInt("id");;
          String nombre = rs.getString("nombre");
          int temp = rs.getInt("complete_control");
          boolean cc;
          if (temp == 1)
              cc = true;
          else
              cc = false;
          Rol r = new Rol(conn,id,nombre,cc);
          if (r.getId() != Rol.SYSOP)
        	  l.add(r);
      }
      return(l);
    }

    
    @Override
    public void guardarNuevo(Connection conn) throws SQLException
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    
}
